/**
 * @Author yinyunqi
 * @date 2017年12月22日
 * @Content 家庭信息处理服务
 */
package com.yongqi.service;

import java.util.List;
import java.util.Map;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import com.yongqi.mapper.FamilyMapper;
import com.yongqi.model.Family;

@Service
@CacheConfig(cacheNames="familyCache")
public class FamilyService {
    @Autowired
    private FamilyMapper familyMapper;  
    //测试
    public void insertFamily() {
	//新建一个家庭对象，给该对象赋值
	Family family = new Family();
	family.setFamilyname("name");
	family.setFamilyaccount("account");
	family.setFamilypassword("password");
	family.setStates("states");
	family.setCreated_time("time");
	familyMapper.insertFamily(family);  //将已赋值的家庭对象传递mapper给接口
    }
    //注册家庭账户
    public void registerFamily(Family family) {
	String familypassword = new SimpleHash("MD5",family.getFamilypassword(),family.getFamilyaccount(),2).toHex();
	family.setFamilypassword(familypassword);
	familyMapper.registerFamily(family);
    }
    //验证家庭账号是否已存在
    public String checkFamilyaccount(String famliyaccount) {
	Map<String, String> family = familyMapper.queryFamilyByAccount(famliyaccount);
	if (null!=family) {
	    return "exist";
	}else {
	    return "empty";
	}
    }
    public Family getFamilyByAccount(String familyaccount) {//shiro验证登录时根据账号获取该Family完整对象
	return familyMapper.getgetFamilyByAccount(familyaccount);
    }
    /**
     * 上传头像图片
     * @param map
     */
    public void familyUploadIcon (Map<String, String> map) {
	familyMapper.familyUploadIcon(map);
    }
    /**
     * 修改家庭用户头像
     * @param map
     */
    public void familyEditIcon (Map<String, String> map) {
	familyMapper.familyEditIcon(map);
    }
    public void familyEditInfo (Map<String, String> map) {
	familyMapper.familyEditInfo(map);
    }
    //后台管理查询用户信息
    public List<Family> backFamilyQuery(Map<String, String> map){
    	return familyMapper.backFamilyQuery(map);
    }
    //后台管理查询用户地址信息
    public List<Family> backFamilyDistributionData(){
	return familyMapper.backFamilyDistributionData();
    }
    public void emailValidation (Map<String, String> map) {
    	familyMapper.emailValidation(map);
    }
    public void changePwd(Map<String, String> map) {
    	String password = map.get("password");
    	password = new SimpleHash("MD5",map.get("password"),map.get("familyaccount"),2).toHex();
    	map.put("password", password);
    	familyMapper.changePwd(map);
    }
}

